//--------------------------------------------------------------------------------------
// File: Tutorial07.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register( s0 );
Texture3D voxelTexture : register( t1 );
SamplerState voxelSamplerLinear : register( s1 );

cbuffer cbNeverChanges : register( b0 )
{
    matrix View;
};

cbuffer cbChangeOnResize : register( b1 )
{
    matrix Projection;
};

cbuffer cbChangesEveryFrame : register( b2 )
{
    matrix World;
};


//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = input;
    /*output.Pos = mul( input.Pos, World );
    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );
    output.Tex = input.Tex;*/
    
    return output;
}
//--------------------------------------------------------------------------------------
// Geometry Shader
//--------------------------------------------------------------------------------------
float4 MultiplyMatrices( float4 input )
{
    float4 output = input;
    output = mul( input, World );
    output = mul( output, View );
    output = mul( output, Projection );

    
    return output;
}
[maxvertexcount(6)]
void GS( point PS_INPUT sprite[1], inout TriangleStream<PS_INPUT> triStream )
{
	PS_INPUT v1,v2,v3,v4,v5,v6;
	

	
	v1.Tex = sprite[0].Tex;
	v2.Tex = sprite[0].Tex;
	v3.Tex = sprite[0].Tex;
	v1.Tex.x = 0.0f;
	v1.Tex.y = 0.2f;
	
	v2.Tex.x = 0.3f;
	v2.Tex.y = 0.4f;
	
	v3.Tex.x = 0.5f;
	v3.Tex.y = 0.6f;
	
	float4 p0, p1, p2;
	
	float texelSize = 1.0f / 2.0f;
	float3 voxelPos = float3(sprite[0].Pos.x, sprite[0].Pos.y, sprite[0].Pos.z);
	p0 = sprite[0].Pos;
	p1 = sprite[0].Pos;
	p2 = sprite[0].Pos;
	
	float scale = voxelTexture.SampleLevel(voxelSamplerLinear, texelSize * voxelPos, 0).r * 255*3;

	//scale = 1;
	float f = 0.04;
	p0 += float4(0, 1, 0, 0) * f * scale;
	p1 += float4(1, -1, 0, 0) * f * scale;
	p2 += float4(-1, -1, 0, 0) * f * scale;
	
	/*p0 = MultiplyMatrices(p0);
	p1 = MultiplyMatrices(p1);
	p2 = MultiplyMatrices(p2);*/
	
	v1.Pos = p0;
	v2.Pos = p1;
	v3.Pos = p2;
	
	triStream.Append(v1);
	triStream.Append(v2);
	triStream.Append(v3);
	triStream.RestartStrip();
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input) : SV_Target
{
    return txDiffuse.Sample( samLinear, input.Tex );// * vMeshColor;
}
